from abc import ABC, abstractmethod
from typing import Union, List, Dict, Any, Callable, Optional, Tuple, Literal, Generic, TypeVar
from enum import Enum
from abc import ABC, abstractmethod
import time

# 写入操作的类型定义
AddressType = Union[str, int]
ValueType = Any
WriteItemType = Tuple[AddressType, ValueType]
WriteItemFormat1 = Tuple[AddressType, ValueType, str]  # (address, value, data_type)
WriteItemFormat3 = Tuple[AddressType, ValueType, str,str]  # (address, value, data_type, option)
WriteItemFormat2 = Tuple[AddressType, ValueType, str, int]  # (address, value, data_type, length/max_value)
BatchWriteType = Union[Dict[AddressType, ValueType], List[WriteItemType], List[Union[WriteItemFormat1, WriteItemFormat2, WriteItemFormat3]]]
ConfigWriteType = Dict[str, Any]
FullWriteType = Union[WriteItemType, Dict[AddressType, ValueType], List[WriteItemType], ConfigWriteType, List[Union[WriteItemFormat1, WriteItemFormat2, WriteItemFormat3]]]

# 读取操作的类型定义
ReadItemType = AddressType
ReadItemFormat1 = Tuple[AddressType, str]  # (address, data_type)
ReadItemFormat2 = Tuple[AddressType, str, int]  # (address, data_type, length/max_value)
ReadItemFormat3 = Tuple[AddressType, str, str]  # (address, data_type, option)
BatchReadType = Union[
    List[ReadItemType],
    List[ReadItemFormat1],
    List[ReadItemFormat2],
    List[ReadItemFormat3]
]
ConfigReadType = Dict[str, Any]
FullReadType = Union[
    ReadItemType,
    List[ReadItemType],
    BatchReadType,
    ConfigReadType
]

# 数据类型枚举
class DataType(Enum):
    NULL = "Null"
    BOOLEAN = "Boolean"
    SBYTE = "SByte"
    BYTE = "Byte"
    INT16 = "Int16"
    UINT16 = "UInt16"
    INT32 = "Int32"
    UINT32 = "UInt32"
    INT64 = "Int64"
    UINT64 = "UInt64"
    FLOAT = "Float"
    DOUBLE = "Double"
    STRING = "String"
    DATETIME = "DateTime"
    GUID = "Guid"
    BYTESTRING = "ByteString"
    XMLELEMENT = "XmlElement"
    NODEID = "NodeId"
    EXPANDEDNODEID = "ExpandedNodeId"
    STATUSCODE = "StatusCode"
    QUALIFIEDNAME = "QualifiedName"
    LOCALIZEDTEXT = "LocalizedText"
    EXTENSIONOBJECT = "ExtensionObject"
    DATAVALUE = "DataValue"
    VARIANT = "Variant"
    DIAGNOSTICINFO = "DiagnosticInfo"
    AUTO = "auto"

# 写入选项枚举
class WriteOption(Enum):
    SYNC = "sync"
    ASYNC = "async"

# 读取选项枚举
class ReadOption(Enum):
    SYNC = "sync"
    ASYNC = "async"

# 处理函数类型
PreProcessorType = Callable[[Any], Any]  # 写入前处理
PostProcessorType = Callable[[bool], Any]  # 写入后处理
PreReaderType = Callable[[Any], Any]  # 读取前处理
PostReaderType = Callable[[Any], Any]  # 读取后处理